// pages/index/index.js
var util = require("../../utils/util.js");
Page({
    /**
     * 页面的初始数据
     */
    data: {
        indicatorDots: true,
        vertical: false,
        autoplay: true,
        interval: 4000,
        duration: 500,
        page: 1,
        classList: ['one-user', 'two-user', 'three-user', 'four-user', 'five-user']
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getBannerList();
        this.getGoodsList();
        this.getFocusList();
        this.getCardList();
    },
    getBannerList: function() {
        const _this = this;
        util.request(getApp().api.index.banner, { type: 1 }).then(
            function(res) {
                if (res.code == 200) {
                    _this.setData({
                        banner_list: res.data
                    });
                    if (res.data.length <= 1) {
                        _this.setData({
                            indicatorDots: false
                        })
                    }
                } else {
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        icon: "none", //图标,
                        //duration: 1500, //延迟时间,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function() {
                        wx.hideToast();
                    }, 1500);
                    return
                }
            }
        );
    },
    getCardList: function() {
        const _this = this;
        util.request(getApp().api.index.card).then(
            function(res) {
                if (res.code == 200) {
                    _this.setData({
                        card_list: res.data
                    });
                } else {
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        icon: "none", //图标,
                        //duration: 1500, //延迟时间,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function() {
                        wx.hideToast();
                    }, 1500);
                    return
                }
            }
        );
    },
    getGoodsList: function() {
        const _this = this;
        util.request(getApp().api.index.product).then(
            function(res) {
                if (res.code == 200) {
                    _this.setData({
                        goods_list: res.data
                    });
                } else {
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        icon: "none", //图标,
                        //duration: 1500, //延迟时间,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function() {
                        wx.hideToast();
                    }, 1500);
                    return
                }
            }
        );
    },
    getFocusList: function() {
        const _this = this;
        util.request(getApp().api.index.focus, { page: _this.data.page }).then(
            function(res) {
                if (res.code == 200) {
                    if (_this.data.page == 1) {
                        _this.setData({
                            focus_list: res.data
                        });
                    } else {
                        _this.setData({
                            focus_list: _this.data.focus_list.concat(res.data)
                        });
                    }

                } else {
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        icon: "none", //图标,
                        //duration: 1500, //延迟时间,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function() {
                        wx.hideToast();
                    }, 1500);
                    return
                }
            }
        );
    },
    toGoodsDetail: function(e) {
        const productId = e.currentTarget.dataset.id;
        const productNum = e.currentTarget.dataset.num;
        wx.navigateTo({
            url: "/pages/goods-detail/goods-detail?productId=" + productId + '&productNum=' + productNum
        })
    },
    toCard: function() {
        wx.navigateTo({
            url: '/pages/card/card'
        })
    },
    toGiftList: function() {
        wx.navigateTo({
            url: '/pages/gift-goods-list/gift-goods-list'
        })
    },
    toDrinkButler: function() {
        wx.navigateTo({
            url: '/pages/drink-butler/drink-butler'
        })
    },
    toArticle: function(e) {
        const articleId = e.currentTarget.dataset.id;
        const userAvatar = wx.getStorageSync('userInfo').avatar;
        util.request(getApp().api.index.addParticipate, { operator: 0, articleId: articleId, avatar: userAvatar }, 'POST').then(
            function(res) {
                if (res.code == 200) {
                    wx.navigateTo({
                        url: "/pages/article-detail/article-detail?article_id=" + articleId
                    })
                } else {
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        image: '/images/goods-pic.png',
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function() {
                        wx.hideToast();
                    }, 1500);
                    return
                }
            }
        );
    },
    imgLarge: function(e) {
        const currentUrl = e.currentTarget.dataset.current;
        const imgUrl = e.currentTarget.dataset.urls;
        wx.previewImage({
            current: currentUrl, // 当前显示图片的http链接
            urls: imgUrl // 需要预览的图片http链接列表
        })
    },
    onReachBottom: function() {
        this.data.page++;
        this.getFocusList();
    },
    toFocus: function(e) {
        const _this = this;
        const articleId = e.currentTarget.dataset.id;
        const userAvatar = wx.getStorageSync('userInfo').avatar;
        console.log(userAvatar);
        util.request(getApp().api.index.addParticipate, { operator: 1, articleId: articleId, avatar: userAvatar }, 'POST').then(
            function(res) {
                if (res.code == 200) {
                    wx.showToast({
                        title: '关注成功', //提示的内容,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function() {
                        wx.hideToast();
                        _this.getFocusList();
                    }, 1500);
                } else {
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        image: '/images/goods-pic.png',
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function() {
                        wx.hideToast();
                    }, 1500);
                    return
                }
            }
        );
    },
    toGreetingCardList: function(e) {
        var goodsInfo = e.currentTarget.dataset.info;
        if (goodsInfo) {
            wx.setStorageSync("giveOrderItem", JSON.stringify({
                productId: goodsInfo.id, //产品ID
                productNum: goodsInfo.productNum, //产品数量
            }));
            wx.navigateTo({
                url: "/pages/greeting-card-list/greeting-card-list"
            })
        } else {
            wx.navigateTo({
                url: "/pages/greeting-card-list/greeting-card-list?origin=" + 'littleThought'
            })
        }

    },
    chooseThis: function(e) {
        const _this = this;
        var currentId = e.currentTarget.dataset.id;
        var cardEnvelopeUrl = e.currentTarget.dataset.envelop;
        wx.setStorageSync("giveCardId", currentId);
        wx.navigateTo({
            url: "/pages/greeting-card/greeting-card?id=" + currentId + '&envelopUrl=' + cardEnvelopeUrl + '&origin=' + 'littleThought'
        })
    },
    toGoodsRecommend: function(e) {
        let userInfo = wx.getStorageSync('userInfo');
        if(userInfo && userInfo.name == '立辉'){
            wx.navigateTo({
                url: "/pages/goods-recommend/goods-recommend?friendId=oOPwp47i1yv08eWdWgXbhqxY7taS"
            })
        }
        wx.navigateTo({
            url: "/pages/goods-personal/goods-personal?friendId=oOPwp47i1yv08eWdWgXbhqxY7taS"
        })
    },
    confirm: function() {
        this.setData({
            isShowFreight: !1
        })
    },
    onReady: function() {}
});